/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;
import java.io.*;
import java.net.*;
import java.util.*;
import data.Login;
import data.Recipient;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author michaelharville
 */

public class LoginServer {
   public static void main(String[] args)
   {
        ObjectInputStream in = null;
        ObjectOutputStream out = null;
        Connection connection = null;  
        Statement stat = null;
        ResultSet result = null;
        
        ArrayList<Recipient> recipients;
        String sql;

      try
      {
            ServerSocket s = new ServerSocket(8189);
           
            while (true) 
            {
                Socket incoming = s.accept();
                recipients = new ArrayList<Recipient>();

                try
                {
                    //load database
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection("jdbc:mysql://LOCALHOST/FileTransfer","root","root");
                    
                    //input stream
                    in = new ObjectInputStream(incoming.getInputStream());
                    
                    //read from client
                    Login login = (Login) in.readObject();
                    
                    /* Check login credentials */
                    
                    stat = connection.createStatement();
                    sql = "SELECT password FROM users "
                            + "WHERE username='" + login.getUsername() + "'";
                    result = stat.executeQuery(sql);
                    
                    if(result.next())
                    {
                        if(login.getPassword().equals(result.getObject(1).toString()))
                        {
                            login.setAuth(true);
                        }
                    }
                    
                    //output stream
                    out = new ObjectOutputStream(incoming.getOutputStream());
                    
                    //send login response to client
                    out.writeObject(login);
                    
                    //if user is authenticated retrieve recipients
                    if(login.getAuth())
                    {
                        /* Get all of the recipients for a user */

                        stat = connection.createStatement();
                        sql = "SELECT r.recipient, u.ip_address "
                                + "FROM recipients r "
                                + "INNER JOIN users u "
                                + "ON r.recipient = u.username "
                                + "WHERE r.username='" + login.getUsername() + "'";
                        result = stat.executeQuery(sql);

                        /* Loop through sql results and create recipient objects */

                        while(result.next())
                        {
                            String user = result.getObject(1).toString();
                            String ip = result.getObject(2).toString();
                            Recipient r = new Recipient(user, ip);
                            recipients.add(r);
                        }
                        
                        //update ip address
                        stat = connection.createStatement();
                        sql = "UPDATE users "
                                + "SET ip_address='" + login.getIPAddress() + "' "
                                + "WHERE username='" + login.getUsername() + "'";
                        stat.execute(sql);

                        //send recipients to client
                        out.writeObject(recipients);
                    }
                }
                catch (Exception e)
                {
                   e.printStackTrace();
                }
                finally
                {
                    in.close();
                    out.close();
                    incoming.close();
                }
            }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}
